package com.internetCafes.spms.common.AIRecognize.handler.impl;

import com.internetCafes.spms.common.AIRecognize.handler.WordHandler;
import com.internetCafes.spms.common.CommonConstants;
import com.internetCafes.spms.web.certificate.vo.AIRecognizeInfo;
import com.internetCafes.spms.web.sys.model.ConfInfo;
import com.internetCafes.spms.web.sys.service.ConfInfoService;
import com.sms.common.util.hutool.core.collection.CollectionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Component
public class QualiWordHandler implements WordHandler {

    private static final Pattern PATTERN = Pattern.compile("[A-Z][0-9]{9}");

    @Autowired
    ConfInfoService confInfoService;

    @Override
    public AIRecognizeInfo handle(String word, String filePath) {
        AIRecognizeInfo aiRecognizeInfo = new AIRecognizeInfo();
        String code = handleCode(word);
        if (code != null) {
            aiRecognizeInfo.setCode(code);
        }
        List<Long> qualification = handleQualification(word);
        if (CollectionUtil.isNotEmpty(qualification)) {
            aiRecognizeInfo.setQualification(qualification);
        }
        List<Integer> levels = handleLevels(word);
        if (CollectionUtil.isNotEmpty(levels)) {
            aiRecognizeInfo.setLevels(levels);
        }
        String registeredCapital = handleRegisteredCapital(word);
        if (registeredCapital != null) {
            aiRecognizeInfo.setRegisteredCapital(Double.parseDouble(registeredCapital));
        }
        String corporateName = handleCorporateName(word);
        if (corporateName != null) {
            aiRecognizeInfo.setCorporateName(corporateName);
        }
        String registeredAddress = handleRegisteredAddress(word);
        if (registeredAddress != null) {
            aiRecognizeInfo.setRegisteredAddress(registeredAddress);
        }
        Date[] dates = handleDate(word);
        if (dates != null) {
            aiRecognizeInfo.setReleaseDate(dates[0]);
            aiRecognizeInfo.setExpireDate(dates[1]);
        }
        return aiRecognizeInfo;
    }

    public String handleCode(String word) {
        Matcher matcher = PATTERN.matcher(word);
        if (matcher.find()) {
            return matcher.group(0);
        }
        return null;
    }

    public List<Long> handleQualification(String word) {
        List<Long> result = new ArrayList<>();
        String[] split = word.split("\n");
        Map<String, Object> params;
        for (String s : split) {
            params = new HashMap<>();
            String line = s;
            if (line.contains(CommonConstants.QualificationLevels.LABOR_SUBCONTRACT)||
                    line.contains(CommonConstants.QualificationLevels.GENERAL_CONTRACTING)||
                    line.contains(CommonConstants.QualificationLevels.PROFESSION_CONTRACTING)) {
                int prefix = line.indexOf(":");
                if (prefix == -1) {
                    prefix = line.indexOf("：");
                }
                if (prefix != -1) {
                    line = line.substring(prefix + 1);
                }
                if (line.contains(CommonConstants.QualificationLevels.LABOR_SUBCONTRACT)){
                    line=line.substring(0,line.length()-"不分等级".length());
                }else {

                    line=line.substring(0,line.indexOf("级")-1);
                }
                params.put("value", line);
                List<ConfInfo> query = confInfoService.query(params);
                if (CollectionUtil.isNotEmpty(query)) {
                    result.add(query.get(0).getId());
                }
            }
        }
        return result;
    }

    public List<Integer> handleLevels(String word) {
        List<Integer> result = new ArrayList<>();
        String[] split = word.split("\n");
        Map<String,Object> params;
        for (String s : split) {
            String line = s;
            params = new HashMap<>();
            if (line.contains(CommonConstants.QualificationLevels.LABOR_SUBCONTRACT)||
                    line.contains(CommonConstants.QualificationLevels.GENERAL_CONTRACTING)||
                    line.contains(CommonConstants.QualificationLevels.PROFESSION_CONTRACTING)) {
                int prefix = line.indexOf(":");
                if (prefix == -1) {
                    prefix = line.indexOf("：");
                }
                if (prefix != -1) {
                    line = line.substring(prefix + 1);
                }
                if (line.contains(CommonConstants.QualificationLevels.LABOR_SUBCONTRACT)){
                    line=line.substring(0,line.length()-"不分等级".length());
                }else {

                    line=line.substring(0,line.indexOf("级")-1);
                }
                params.put("value", line);
                List<ConfInfo> query = confInfoService.query(params);
                if (CollectionUtil.isNotEmpty(query)) {
                    Map<Integer, String> descrMap = CommonConstants.QualificationLevels.descrMap;
                    for (Map.Entry<Integer, String> entry : descrMap.entrySet()) {
                        if (s.contains(entry.getValue())) {
                            result.add(entry.getKey());
                            break;
                        }
                    }
                }
            }
        }
        return result;
    }

    public String handleRegisteredCapital(String word) {
        String[] split = word.split("\n");
        String result = null;
        for (String line : split) {
            if (line.contains("人民币")) {
                int start = line.indexOf(":");
                int end = line.indexOf("万");
                if (start == -1) {
                    start = line.indexOf("：");
                }
                if (start != -1) {
                    result = line.substring(start + 1, end);
                } else {
                    result = line.substring(0, end);
                }
            }
        }
        return result;
    }

    public String handleCorporateName(String word) {
        String[] split = word.split("\n");
        String result = null;
        for (String line : split) {
            if (line.contains("法定代表人")) {
                int start = line.indexOf(":");
                if (start == -1) {
                    start = line.indexOf("：");
                }
                result = line.substring(start + 1);
            }
        }
        return result;
    }

    public String handleRegisteredAddress(String word) {
        String[] split = word.split("\n");
        String result = null;
        for (String line : split) {
            if (line.contains("地址")) {
                int start = line.indexOf(":");
                if (start == -1) {
                    start = line.indexOf("：");
                }
                result = line.substring(start + 1);
            }
        }
        return result;
    }

    public Date[] handleDate(String word) {
        word = word.replace("\n", "");
        Date[] result = new Date[2];
        String reg = "\\d{4}年\\d{1,2}月\\d{1,2}日";
        Pattern pattern = Pattern.compile(reg);
        Matcher matcher = pattern.matcher(word);
        if (matcher.find()) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
            Date firstDate = null;
            Date secondDate = null;
            try {
                firstDate = sdf.parse(matcher.group());
            } catch (ParseException e) {
                e.printStackTrace();
            }
            if (matcher.find()) {
                try {
                    secondDate = sdf.parse(matcher.group());
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                if (firstDate.before(secondDate)) {
                    result[0] = firstDate;
                    result[1] = secondDate;
                } else {
                    result[1] = firstDate;
                    result[0] = secondDate;
                }
                return result;
            }
            result[0] = firstDate;
            return result;
        }
        return null;
    }
}
